var clmannest = {
	rules:[
		{
			site:/cl\.man\.lv\/thread0806\.php\?fid=(21|2|15|4|5)/,
			nest:[{
					selector:".tr3 td:nth-child(3)",
					nestFun:"addDownPicExt"
				}
			],
			picReg:/img.*?src\s*=\s*['"](.*?)['"]/g
		},
		{
			site:/cl\.man\.lv\/thread0806\.php\?fid=(8|16)/,
			nest:[{
					selector:".tr3 td:nth-child(3)",
					nestFun:"addDownPicExt"
				}
			],
			picReg:/input.*?src\s*=\s*['"](.*?)['"]/g
		}
	],
	currentRule:null,
	addDownPicExt: function (containers, styles){
		// if(containers instanceof jQuery)
		var downpic = '<div class="btn-wrapper" style="position: absolute;  margin-left: -82px; margin-top: -32px; text-align: left; color: rgb(47, 95, 161);">'+
						'<a class="preview" href="javascript:void(0)" style="padding: 2px 0px;display:block;visibility:hidden;">查看图片</a>'+
						'<span style="border-color: cadetblue;display:block;visibility:hidden;">加入列表<input type="checkbox"></span>'+
					  '</div>'
		// var defaultStyle = {
		// 	wrapper:{
		// 		position: "absolute",
		// 		"margin-left": "-118px",
		// 		"margin-top": "-26px"
		// 	}
		// 	// download:{
		// 	// 	padding: "0 10px"
		// 	// },
		// 	// showpic:{

		// 	// }
		// }
		// !styles && (styles = defaultStyle);
		var _this = this;
		!this.pic && (this.pic = new PIC("http://localhost:8088/ad/getAds.php"));
		var list = [];
		containers.each(function(i, it){
			var wrapper = $(downpic).appendTo($(it));
			// wrapper.find(".download").css(styles.download);
			// wrapper.find(".showpic").css(styles.showpic).bind("click", function(){
			// 	var urlA = $(this).parent().closest("td").find("a");
			// 	util.getRespText(urlA.length >0? urlA[0].href:"", function(respText){
			// 		var reg = clmannest.currentRule.picReg;
			//         var picUrls = util.getPicUrl(respText, reg);
			//         _this.pic.addSerial(urlA.eq(0).text() , picUrls);
			//         _this.pic.show();
			//         _this.pic.setSize(document.body.clientWidth*0.33,document.body.clientWidth*0.33);
			// 	});
			// });
			// $(it).hover(function(){

			// 	$(this).find(".btn-wrapper").show().find(".showpic").text(surfing_helper_globle?"放入图片列表":"查看图片");
			// },function(){
			// 	$(this).find(".btn-wrapper").hide()
			// })
			wrapper.find("a").bind("click", function(){
				var urlA = $(this).closest("tr").find("h3 a");
				if(util.inArray(list, this) !== -1){
			        _this.pic.showSerial(urlA.eq(0).text());
				}else{
					util.getRespText(urlA.length >0? urlA[0].href:"", function(respText){
						var reg = clmannest.currentRule.picReg;
				        var picUrls = util.getPicUrl(respText, reg);
				        _this.pic.addSerial(urlA.eq(0).text() , picUrls);
				        _this.pic.showSerial(urlA.eq(0).text());
				        _this.pic.setSize(document.body.clientWidth*0.33,document.body.clientWidth*0.33);
				        $(_this.pic.picWrapper).find("#pic_serial_list ul").bind("click", function(event){
				        	var ele = event.target;
				        	if(ele.parentNode.tagName == "LI"){
				        		var index = $(ele.parentNode).index();
				        		for(var i = 0 ; i < list.length; i++){
				        			if(list[i].getAttribute("_title") == $(ele.parentNode).text()){
				        				list[i].textContent = "查看中";
				        				$(list[i]).css("visibility", "visible");
				        			}else{
					        			list[i].textContent = "查看图片";
					        			$(list[i]).css("visibility", "hidden");
				        			}
				        		}
				        	}
				        })
				        $(_this.pic.picWrapper).find(".pic-close").bind("click", function  (argument) {
			        		for(var i = 0 ; i < list.length; i++){
			        			if(list[i].textContent == "查看中"){
			        				list[i].textContent = "查看图片";
				        			$(list[i]).css("visibility", "hidden");
				        		}
			        		}
				        })
					});
					this.setAttribute("_title", urlA.eq(0).text());
					list.push(this);
				}
				$(this).parent().find("input")[0].checked = true;
        		for(var i = 0 ; i < list.length; i++){
        			if(list[i].textContent == "查看中"){
	        			list[i].textContent = "查看图片";
	        			$(list[i]).css("visibility", "hidden");
        			}
        		}
				$(this).text("查看中");
			})
			wrapper.find("input").bind("click", function(){
				var tmpEle = $(this).closest(".btn-wrapper").find(".preview")[0];
				if(util.inArray(list, tmpEle) == -1){
					var urlA = $(this).closest("tr").find("h3 a");
					util.getRespText(urlA.length >0? urlA[0].href:"", function(respText){
						var reg = clmannest.currentRule.picReg;
				        var picUrls = util.getPicUrl(respText, reg);
				        _this.pic.addSerial(urlA.eq(0).text() , picUrls);
					});
					tmpEle.setAttribute("_title", urlA.eq(0).text());
					list.push(tmpEle);
				}
			})
		})
		var timeout;
		$("#ajaxtable tr").hover(function(){
			var ele = this;
			setTimeout(function() {
				$(ele).find(".btn-wrapper a,.btn-wrapper span").css("visibility", "visible");

			}, 200);
		}, function(){
			var ele = this;
			setTimeout(function() {
				var tmp = $(ele).find(".btn-wrapper a");
				if(tmp.text() !== "查看中"){
					$(ele).find(".btn-wrapper a").css("visibility", "hidden");
				}
				tmp = $(ele).find(".btn-wrapper input");
				if(!tmp[0].checked){
					tmp.parent().css("visibility", "hidden");
				}
			}, 200);
		})
	},
	run: function(){
		var rules = this.rules;
		var rule = null;
		for(var i = 0; i < rules.length; i++){
			rule = rules[i];
			if(location.href.match(rule.site)){
				this.currentRule = rule;
				var nest = rule.nest;
				for(var j = 0; j < nest.length; j++){
					clmannest[nest[j].nestFun]($(nest[j].selector));
				}
				break;
			}
		}
	}
}
var pp = {
	picUrls:[],
	builded: 0,
	frameEle:null,
	contentPic: null,
	currentPicIndex:0,
	build: function(){
		if(this.builded == 0){
			adapter.insertCSS("static/css/picshow.css");

			var frame = "<div class='pic-wrapper'>"+
							"<a href='javascript:void(0)' class='picshow-close-button'>"+
								"<img src='"+static_imgs.picshowclose+"'/>"+
							"</a>"+
							"<div class='pic-container'>"+
								"<div class='align-div'/>"+
								"<img/>"+
								"<div class='align-div'/>"+
							"</div>"+
							"<div class='control-tools'>"+
								"<a href='javascript:void(0)' class='prev'>上一张</a>"+
								"<a href='javascript:void(0)' class='next'>下一张</a>"+
							"<div/>"+
						"</div>";
			var styles={
				"wrapper":{
					position: "fixed",
					right: "0",
					top: "0",
					width: "32%",
					height: window.innerWidth*0.33+50,
					background: "aliceblue",
					border: "solid 2px #abc",
					padding: "5px"
				},
				other:[
				]
			};
			var frameEle = this.frameEle = $(frame).appendTo($(document.body));
			this.contentPic = frameEle.find(".pic-container img");
			frameEle.css(styles.wrapper);
			var other = styles.other;
			for(var i = 0; i < other.length; i++ ){
				frameEle.find(other[i].selector).css(other[i].style);
			}
			if(pp.picUrls.length > 1){
				frameEle.find(".next").css({
						color:"#2f5fa1",
						cursor:"pointer"
					})
			}
			frameEle.find(".prev").bind("click", function(){
				pp.currentPicIndex--;
				if(pp.currentPicIndex == -1) {
					pp.currentPicIndex++;
					return;
				}
				if(pp.currentPicIndex == 0) {
					$(this).css({
						color:"grey",
						cursor:"default"
					});
				}
				if(pp.currentPicIndex == pp.picUrls.length - 2) {
					$(this).next().css({
						color:"#2f5fa1",
						cursor:"pointer"
					});
				}
				if(pp.currentPicIndex >= 0){
					pp.contentPic.attr("src", pp.picUrls[pp.currentPicIndex]).attr("onerror",  pp.getBakpicture());
				}
			})
			frameEle.find(".next").bind("click", function(){
				pp.currentPicIndex++;
				if(pp.currentPicIndex == pp.picUrls.length) {
					pp.currentPicIndex--;
					return;
				}
				if(pp.currentPicIndex == pp.picUrls.length - 1) {
					$(this).css({
						color:"grey",
						cursor:"default"
					});
				}
				if(pp.currentPicIndex == 1) {
					$(this).prev().css({
						color:"#2f5fa1",
						cursor:"pointer"
					});
				}
				if(pp.currentPicIndex < pp.picUrls.length){
					pp.contentPic.attr("src", pp.picUrls[pp.currentPicIndex]).attr("onerror",  pp.getBakpicture());

				}
			})
			frameEle.find(".picshow-close-button").bind("click", function(){
				frameEle.hide();
			})
			this.builded = 1;
		}
		this.showPictures(this.picUrls);
	},
	showPictures: function(picUrls){
		if(picUrls && picUrls.length > 0){
			this.contentPic.attr("src", picUrls[0]);
		}else{
			this.contentPic.attr("src", this.getBakpicture());
		};
		this.frameEle.show();
	},
	getBakpicture: function(){
		var bakpictures=[
			"http://img02.taobaocdn.com/imgextra/i2/740783227/T2GKX_X0NaXXXXXXXX-740783227.jpg",
			"http://img01.taobaocdn.com/imgextra/i1/740783227/TB2z4LjXpXXXXb1XpXXXXXXXXXX-740783227.jpg",
			"http://img03.taobaocdn.com/imgextra/i3/854073895/T2hx8fX48aXXXXXXXX-854073895.jpg"
		]
		var i = Math.ceil(Math.random()*bakpictures.length);
		return bakpictures[i];
	},
	run: function(){
		pp.currentPicIndex = 0;
		this.build();
		this.showStartPic();
	},
	showStartPic: function(){
		this.showPic(this.getBakpicture());
	},
	showPic:function(url){
		this.contentPic.attr("src", url);
	},
	changePicSet: function(){
		this.currentPicIndex = 0;
		this.show
	}

}
